package com.xiaotu.chess;

import javax.swing.*;
import java.awt.*;
import java.io.File;

/**
 * 在swing编程中面板是指JPanel
 * 在JFrame自带有一个面板，但那个面板有bug
 * <p>
 * 关于Java的继承
 * 注意：
 * 1.父类是必须先存在的
 * 2.子类只能继承父类中的非私有的属性和方法
 * 3.在同一个包下，子类可以继承父类中的默认的属性和方法
 * 4.在不同的包下，子类不能继承父类中的默认的属性和方法，只能继承被保护的和公共的
 *
 * @author 张晓
 * @create 2021-10-10 21:37
 */
public class ChessPanel extends JPanel {
    @Override
    public void paint(Graphics g) {
//        super.paint(g);
        /**
         * Graphics 绘制类
         *  常用方法：
         *      g.drawImage():画图片
         *      g.drawChars():画文字
         *      g.drawLine():画直线
         *      g.drawOval:画圆或椭圆
         */
        String bgPath = "pic" + File.separator + "qipan.jpg";
        Image bgImage = Toolkit.getDefaultToolkit().getImage(bgPath);
        g.drawImage(bgImage, 0, 0, this);
        //画棋子
       /* String che0Path = "pic" + File.separator + "che0.png";
        Image che0Image = Toolkit.getDefaultToolkit().getImage(che0Path);
        g.drawImage(che0Image, 5, 5, 30, 30, this);
        String ma0Path = "pic" + File.separator + "ma0.png";
        Image ma0Image = Toolkit.getDefaultToolkit().getImage(ma0Path);
        g.drawImage(ma0Image, 45, 5, 30, 30, this);
        String xiang0Path = "pic" + File.separator + "xiang0.png";
        Image xiang0Image = Toolkit.getDefaultToolkit().getImage(xiang0Path);
        g.drawImage(xiang0Image, 85, 5, 30, 30, this);*/

        String[] names = {"che", "ma", "xiang", "shi", "boss", "shi", "xiang", "ma", "che", "pao", "pao", "bing", "bing", "bing", "bing", "bing"};
//        int[] xs = {5, 45, 85, 125, 165, 205, 245, 285, 325, 45, 285, 5, 85, 165, 245, 325};
//        int[] ys = {5, 5, 5, 5, 5, 5, 5, 5, 5, 85, 85, 125, 125, 125, 125, 125};
        int[] xs = {1, 2, 3, 4, 5, 6, 7, 8, 9, 2, 8, 1, 3, 5, 7, 9};
        int[] ys = {1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 3, 4, 4, 4, 4, 4};
        int size = 30;
        int margin = 20;
        int space = 40;
        String suffix = ".png";
        int player = 0;
        for (int i = 0; i < names.length; i++) {
            String path = "pic" + File.separator + names[i] + player + suffix;
            Image image = Toolkit.getDefaultToolkit().getImage(path);
            g.drawImage(image, margin - size / 2 + space * (xs[i] - 1), margin - size / 2 + space * (ys[i] - 1), size, size, this);
        }
        player = 1;
        for (int i = 0; i < names.length; i++) {
            String path = "pic" + File.separator + names[i] + player + suffix;
            Image image = Toolkit.getDefaultToolkit().getImage(path);
            g.drawImage(image, margin - size / 2 + space * (reverseX(xs[i]) - 1), margin - size / 2 + space * (reverseY(ys[i]) - 1), size, size, this);
        }
    }

    private int reverseX(int x) {
        return 10 - x;
    }

    private int reverseY(int y) {
        return 11 - y;
    }

}
